package com.qffx.moviebase.view;


import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

import com.qffx.moviebase.R;
import com.qffx.moviebase.utils.NetWorkUtils;
import com.qffx.moviebase.view.classify.ClassifyFragment;
import com.qffx.moviebase.view.impl.BaseActivity;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup radioGroup;
    private RecommandFragment recommandFragment = new RecommandFragment();
    private ClassifyFragment classifyFragment = new ClassifyFragment();
    private FindFragment findFragment = new FindFragment();
    private MyFragment myFragment = new MyFragment();
    private List<Fragment> list = new ArrayList<>();
    private FragmentTransaction transaction;
    private RadioButton rb_1,rb_2,rb_3,rb_4;
    private long exitTime=0;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initWindow();
        init();

    }

    private void initWindow() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
    }

    private void init() {

        radioGroup = (RadioGroup) findViewById(R.id.main_rg);
        rb_1= (RadioButton) findViewById(R.id.main_recomand);
        rb_2= (RadioButton) findViewById(R.id.main_classify);
        rb_3= (RadioButton) findViewById(R.id.main_find);
        rb_4= (RadioButton) findViewById(R.id.main_my);
        list.add(recommandFragment);
        list.add(classifyFragment);
        list.add(findFragment);
        list.add(myFragment);
        radioGroup.setOnCheckedChangeListener(this);
        transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(R.id.main_frame,recommandFragment).add(R.id.main_frame,classifyFragment).add(R.id.main_frame,findFragment)
                .add(R.id.main_frame,myFragment).show(recommandFragment).hide(classifyFragment).hide(findFragment).hide(myFragment).commit();
        getNetWorkState();


    }


    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int id) {
        switch (id) {
            case R.id.main_recomand:
                showFragment(0);
                break;
            case R.id.main_classify:
                showFragment(1);
                break;
            case R.id.main_find:
                showFragment(2);
                break;
            case R.id.main_my:
                showFragment(3);
                break;
        }
    }

    //展示fragment的方法
    private void showFragment(int position) {

        if(position==0){
           transaction = getSupportFragmentManager().beginTransaction();
           transaction.show(list.get(0)).hide(list.get(1)).hide(list.get(2)).hide(list.get(3)).commit();
        }
        if(position==1){
            transaction = getSupportFragmentManager().beginTransaction();
            transaction.show(list.get(1)).hide(list.get(0)).hide(list.get(2)).hide(list.get(3)).commit();
        }
        if(position==2){
            transaction = getSupportFragmentManager().beginTransaction();
            transaction.show(list.get(2)).hide(list.get(1)).hide(list.get(0)).hide(list.get(3)).commit();
        }
        if(position==3){
            transaction = getSupportFragmentManager().beginTransaction();
            transaction.show(list.get(3)).hide(list.get(1)).hide(list.get(2)).hide(list.get(0)).commit();
        }
    }

    public void tvClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.tv_1:
                rb_1.setChecked(true);
                break;
            case R.id.tv_2:
               rb_1.setChecked(true);
                break;
            case R.id.tv_3:
                rb_2.setChecked(true);
                break;
            case R.id.tv_4:
                rb_2.setChecked(true);
                break;
            case R.id.tv_5:
                rb_3.setChecked(true);
                break;
            case R.id.tv_6:
                rb_3.setChecked(true);
                break;
            case R.id.tv_7:
                rb_4.setChecked(true);
                break;
            case R.id.tv_8:
                rb_4.setChecked(true);
                break;
            default:
                break;
        }
    }

    //获得网络状态
    public void getNetWorkState() {
        if (NetWorkUtils.isNetWorkAvailable(this)) {//有网的情况
            if (NetWorkUtils.getNetWorkState(this) == NetWorkUtils.NetWorkState.WIFI) {
                Toast.makeText(this, "正在使用wifi网络", Toast.LENGTH_SHORT).show();
            } else if (NetWorkUtils.getNetWorkState(this) == NetWorkUtils.NetWorkState.MOBILE)
                Toast.makeText(this, "正在使用2G/3G网", Toast.LENGTH_SHORT).show();
        } else {//没网的情况
            Toast.makeText(this, "没有网络,请连接网络", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN){
            if((System.currentTimeMillis()-exitTime) > 2000){
                Toast.makeText(getApplicationContext(), "再按一次退出程序", Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
                System.exit(0);
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
